import requests
import pytest
import json

target = "http://community.stephenj.me:8082"
cookies = ""
headers = {'Content-Type': "application/x-www-form-urlencoded", 'Accept': "*/*", 'Cache-Control': "no-cache",
           'Accept-Encoding': "gzip, deflate", 'Content-Length': "27", 'Connection': "keep-alive",
           'cache-control': "no-cache", 'Host': "community.stephenj.me:8082", 'Cookie': cookies}


def login(username, password):
    path = "/login"
    payload = {
        'username': username,
        'password': password
    }
    response = requests.request("POST", target + path, data=payload, headers=headers)
    print(response.text)
    print()
    return response


def test_logins():
    """
    账号密码登录测试
    """
    print("\n账号密码登录测试")
    global cookies
    userlists = [
        ["admin", "123"],
        ["libai", "123"],
        ["hanyu", "123"],
        ["liuzongyuan", "123"],
        ["zenggong", "123"],
    ]
    for hr in userlists:
        print("使用用户名为%s, 密码为%s的账号登录" % (hr[0], hr[1]))
        response = login(hr[0], hr[1])
        if hr[0] == "admin":
            cookies = response.cookies
        assert response.status_code == 200


def get_cookie(username="admin", password="123"):
    """
    获取用户的Cookie
    """
    global headers
    co = requests.utils.dict_from_cookiejar(login(username, password).cookies)
    key, value = "", ""
    for key, value in co.items():
        pass
    headers['Cookie'] = key + "=" + value
    return headers


def test_sysmenu():
    """
    系统菜单获取
    """
    print("\n测试菜单的获取")
    print("获取界面首页的目录树")
    path = "/config/sysmenu"
    headers = get_cookie()
    response = requests.request("GET", target + path, headers=headers)
    respjson = json.loads(response.content)[0]
    print("获取的父目录为：" + respjson["name"])
    childstr = ""
    for child in respjson["children"]:
        childstr += child["name"] + "  "
    print("子目录有：" + childstr)
    assert respjson['id'] == 29


def test_hr():
    """
    本用户信息获取
    """
    print("\n本用户信息获取")
    path = "/config/hr"
    headers = get_cookie()
    response = requests.request("GET", target + path, headers=headers)
    print("获取本用户信息为：" + response.text)
    respjson = json.loads(response.content)
    assert respjson['id'] == 3


def test_get_task():
    """
    获取任务测试
    """
    print("\n获取任务测试")
    path = "/member/task"
    headers = get_cookie()
    response = requests.request("GET", target + path, headers=headers)
    respjson = json.loads(response.content)
    for task in respjson:
        print("获取的%3d号任务名为：%10s, \t任务详情为：%30s, \t任务状态为：%s" % (
        task['taskNo'], task['name'], task['content'], task['status']))
    assert respjson is not None


def test_add_task():
    """
    添加任务测试
    """
    print("\n添加任务测试")
    path = "/member/task"
    headers = get_cookie()
    payload = {
        'name': "test34",
        'content': "this is a test add",
        'address': "this is a test address",
        'memberName': "Stephen",
        'memberPhoneNr': "15651635311"
    }
    response = requests.request("POST", target+path, data=payload, headers=headers)
    assert response.status_code == 200

# def test_delete_task():
#     """
#     删除任务测试
#     """
#     print("\n删除任务测试")
#     path = "/member/task/7"
#     headers = get_cookie()
#     response = requests.request("DELETE", target+path, headers=headers)
#     assert response.status_code == 200

def test_update_task():
    """
    更新任务信息
    """
    print("\n更新任务信息测试")
    path = "/member/task"
    payload = {
        'taskNo': 4,
        'name': "test+",
        'content': "there are tests in river",
        'address': "river"
    }
    response = requests.request("PUT", target+path, data=payload, headers=headers)
    assert response.status_code == 200

def test_get_video():
    """
    获取视频链接测试
    """
    print("\n获取视频链接测试")
    path = "/member/vedio"
    headers = get_cookie()
    response = requests.request("GET", target+path, headers=headers)
    respjson = json.loads(response.content)
    for video in respjson:
        print("视频号为：%-3d，视频序号为：%-3d，视频的名字为：%-10s，视频为路径为：%s" % (video['videoNo'], video['serialNr'], video['videoName'], video['path']))
    assert respjson is not None

